package com.lucene5.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.ansj.lucene5.AnsjAnalyzer;
import org.ansj.splitWord.analysis.IndexAnalysis;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * 查询测试
 * 
 * @author famous
 *
 */
public class LuceneQueryTest1 {
	static Analyzer analyzer;
	static Directory d;
	static IndexWriter indexWriter;
	static IndexWriterConfig conf;

	final static String queryKeyWord1 = "华美";

	@BeforeClass
	public static void setup() {
		analyzer = new AnsjAnalyzer();
		analyzer = new AnsjAnalyzer("index");
		try {
			// d = FSDirectory.open(Paths.get("D://index"));
			d = new RAMDirectory();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		conf = new IndexWriterConfig(analyzer);
		try {
			indexWriter = new IndexWriter(d, conf);
		} catch (IOException e) {
			e.printStackTrace();
		}
		InputStream is = LuceneQueryTest1.class.getResourceAsStream("/data/test");
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		try {
			String line = null;
			while ((line = br.readLine()) != null) {
				System.err.println(IndexAnalysis.parse(line));
				Document document = new Document();
				TextField productName = new TextField("productName", line, Store.YES);
				document.add(productName);
				indexWriter.addDocument(document);
			}
			indexWriter.commit();
			indexWriter.close();
			br.close();
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@AfterClass
	public static void teardown() {
		try {
			indexWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//@Test
	public void termQueryTest() {
		System.err.println("termQuery");
		IndexReader reader;
		try {
			reader = DirectoryReader.open(d);
			IndexSearcher searcher = new IndexSearcher(reader);
			Query query = new TermQuery(new Term("productName", queryKeyWord1));
			TopDocs topDocs = searcher.search(query, Integer.MAX_VALUE);
			ScoreDoc[] scoreDocs = topDocs.scoreDocs;
			for (ScoreDoc scoreDoc : scoreDocs) {
				Document document = searcher.doc(scoreDoc.doc);
				System.err.println("TermQuery:" + document.get("productName") + scoreDoc.score);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void queryparseTest1() {
		System.err.println("003");
		IndexReader reader;
		try {
			reader = DirectoryReader.open(d);
			IndexSearcher searcher = new IndexSearcher(reader);
			QueryParser parser = new QueryParser("productName", analyzer);
			Query query = null;
			try {
				query = parser.parse(queryKeyWord1);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			TopDocs topDocs = searcher.search(query, Integer.MAX_VALUE);
			ScoreDoc[] scoreDocs = topDocs.scoreDocs;
			for (ScoreDoc scoreDoc : scoreDocs) {
				Document document = searcher.doc(scoreDoc.doc);
				System.err.println("QueryParser:" + document.get("productName") + scoreDoc.score);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
